package com.lovo.school.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import com.lovo.common.model.CutPageBean;
import com.lovo.common.model.SchoolBean;
import com.lovo.common.model.SysDictBean;
import com.lovo.common.service.ISysDictService;
import com.lovo.common.service.impl.SysDictServiceImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoCutPage;
import com.lovo.netCRM.component.LovoList;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.netCRM.component.LovoTitlePanel;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.school.service.ISchoolService;
import com.lovo.school.service.impl.SchoolServiceImpl;


/**
 * 学校管理主面板
 * @author 白德柯
 *
 */
public class SchoolPanel extends JPanel{

	/**学校表格组件*/
	private LovoTable schoolTable;
	/**窗体组件*/
	private JFrame jf;
	/**员工姓名文本框*/
	private LovoTxt nameTxt;
	/**文化程度文本框*/
	private LovoTxt educationTxt;
	/**所在部门文本框*/
	private LovoTxt deptTxt;
	/**工作职位文本框*/
	private LovoTxt workTxt;
	/**状态下拉框*/
	private LovoComboBox<SysDictBean> statusComboBox;

	/**分页对象*/
	private LovoCutPage cutPage;
	private ISchoolService schoolService = new SchoolServiceImpl();
	private ISysDictService sysDictService = new SysDictServiceImpl();

	/**选中的城市id*/
	private int cityId;

	/**城市列表*/
	private LovoList<SysDictBean> cityList;
	public SchoolPanel(JFrame jf){
		this.jf = jf;
		this.setLayout(null);
		this.init();
	}
	/**
	 * 初始化
	 *
	 */
	private void init() {
		new LovoTitleLabel("学 校 管 理",this);
		this.initTable();
		this.initButton();
		this.initList();
		this.initFindPanel();
		this.initData();
	}
	/**
	 * 初始化数据
	 */
	public void initData(){
		this.updateTable(1);
	}
	/**
	 * 初始化按钮
	 *
	 */
	private void initButton() {
		LovoButton addButton = new LovoButton(50,460,"添加学校","image/add.png",this);
		
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cityId == 0){
					JOptionPane.showMessageDialog(SchoolPanel.this,"请选择对应的城市");
					return;
				}
				new SchoolAddDialog(jf,cityId, SchoolPanel.this);
			}
		});
		
		LovoButton updateButton = new LovoButton(250,460,"修改学校","image/update.png",this);
		
		updateButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new SchoolUpdateDialog(jf, 0, SchoolPanel.this);
			}
		});
		
		LovoButton delButton = new LovoButton(50,525,"删除学校","image/del.png",this);

		LovoButton showButton = new LovoButton(250,525,"查看学校信息","image/find.png",this);
		
		showButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(schoolTable.getKeyByInt());
				new SchoolShowDialog(jf, 0);
			}
		});
		
		LovoButton applyForButton = new LovoButton(50,590,"申请立项","image/find.png",this);
		
		LovoButton checkButton = new LovoButton(250,590,"审核","image/find.png",this);
		
		checkButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new SchoolCheckDialog(jf, 0, SchoolPanel.this);
			}
		});

	}
	/**
	 * 初始化查询按钮
	 *
	 */
	private void initFindPanel(){

		LovoTitlePanel jp = new LovoTitlePanel("查询学校",400, 460, 320, 130,this);
		this.nameTxt = new LovoTxt("学校名称",30,30,jp);
		this.statusComboBox = new LovoComboBox("状态",sysDictService.findByTypeId(1002),30,60,jp);
		
		LovoButton findButton = new LovoButton("查找",180,90,jp);
		findButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SysDictBean statusBean = statusComboBox.getItem();
				System.out.println(statusBean.getId());
				updateTable(1);
			}
		});
		findButton.setSize(60, 20);

		
	}

	
	//-------------------------------------------------
	/**
	 * 初始化表格
	 *
	 */
	private void initTable() {
		schoolTable = new LovoTable(this,
				new String[]{"学校名称","校长","录入时间","状态"},
				new String[]{"name","leader","createDate","statusValue"},//学校实体属性名数组 new String[]{"name","sex"}
				"id");//主键属性名
		schoolTable.setSizeAndLocation(220, 90, 500, 300);
		// 分页
		cutPage = new LovoCutPage(220,400,this) {
			@Override
			public void clickEvent(int arg0) {
				
			}
		};
		
	}
	/**
	 * 初始化列表
	 */
	private void initList() {
		cityList = new LovoList<SysDictBean>(20, 90, 195, 300, sysDictService.findByTypeId(1004), this){
			@Override
			public void onchange(SysDictBean sysDictBean) {
				System.out.println(sysDictBean.getDictValue());
				cityId = sysDictBean.getId();
				updateTable(1);
			}
		};
		cityList.setBorder(BorderFactory.createEtchedBorder());

	}
	/**
	 * 更新表格数据
	 */
	private void updateTable(int page){
		SchoolBean schoolBean = new SchoolBean();
		schoolBean.setCityId(cityId);
		schoolBean.setName(nameTxt.getText());
		schoolBean.setStatus(statusComboBox.getItem().getId());
		CutPageBean cutPageBean = schoolService.findByItem(page,schoolBean);
		//更新表格
		schoolTable.updateLovoTable(cutPageBean.getList());
		if(page == 1){
			cutPage.setTotalPage(cutPageBean.getTotalPage());
		}
	}
	

}
